package game.cube.scene;

import game.cube.Camera;
import game.cube.Cube;
import game.cube.Point;
import game.cube.Shape;
import android.app.Activity;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class Scene1 implements SceneInterface, View.OnTouchListener, View.OnClickListener {

	private enum CAMERA_STATE {
		DEMO,
		START,
		PLAY,
		MAX,
	}

	final float CUBE_SIZE			= 1.0f;
	final float	CAMERA_EYE_X		= 10.0f;
	final float	CAMERA_EYE_Y		= 10.0f;
	final float	CAMERA_EYE_Z		= 10.0f;
	final float TEXT_SIZE			= 64.0f;
	final int	ADJUST_LAYOUT_DEV   = 10;

	private float sceneCamera = 1.0f;
	
	private int				mWidth;
	private int				mHeight;
	private int				mCameraState		= 0;
	private float			mShakeWidth			= 0.0f;
	private float			mAngleX				= 270.0f;
	private float			mAngleY				= 0.0f;
	private float			mCubeSize			= CUBE_SIZE;
	private float[]			mColor				= new float[4];
	private boolean			mShakeFlag			= false;
	private boolean			mCubeSizeFlag		= false;
	private boolean			mChangeScene		= false;
	private Point			mPoints				= new Point();
	private Cube[]			mCube				= new Cube[1000];
	private Camera			mCamera				= new Camera();
	private String			mStartButtonName	= "next";
	private TextView		mTitleText;
	private Activity		mActivity;
	private LinearLayout	mBaseLayout;
	private Button			mStartButton;

	private Handler mHandler = new Handler();
	private Runnable mInit = new Runnable() {

		@Override
		public void run() {
			mBaseLayout = new LinearLayout(mActivity);
			mBaseLayout.setOrientation(LinearLayout.VERTICAL);

			LinearLayout mStartButtonLayout = new LinearLayout(mActivity);

			mStartButton = new Button(mActivity);
			mStartButton.setText(mStartButtonName);
			mStartButton.setTag(mStartButtonName);
			mStartButton.setWidth(mWidth/ADJUST_LAYOUT_DEV);
			mStartButton.setHeight(mHeight/ADJUST_LAYOUT_DEV);
			mStartButton.setOnTouchListener(Scene1.this);
			mStartButtonLayout.addView(mStartButton, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			mBaseLayout.addView(mStartButtonLayout);


			mTitleText = new TextView(mActivity);
			mTitleText.setText("GameDevelopers");
			mTitleText.setTextSize(TEXT_SIZE);
			mBaseLayout.addView(mTitleText);

			mActivity.addContentView(mBaseLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		}

	};
	public Scene1(Activity activity, int displayWidth, int displayHeight) {
		mActivity = activity;
		mWidth = displayWidth;
		mHeight = displayHeight;
		mCube = new Cube[1000];
		for (int x = 0; x < 10; ++x) {
		for (int y = 0; y < 10; ++y) {
		for (int z = 0; z < 10; ++z) {
			mCube[x * 100 + y * 10 + z] = new Cube();
			mCube[x * 100 + y * 10 + z].set_active(true);
			mCube[x * 100 + y * 10 + z].setTranslate(
					Cube.SIZE_X  * x * 2 - Cube.SIZE_X * 5 * 2,
					Cube.SIZE_Y  * y * 2 - Cube.SIZE_Y * 5 * 2,
					Cube.SIZE_Z  * z * 2 - Cube.SIZE_Z * 5 * 2);
			mColor[0] = x * 0.1f;
			mColor[1] = y * 0.1f;
			mColor[2] = z * 0.1f;
			mColor[3] = 1.0f;
			mCube[x * 100 + y * 10 + z].set_color(mColor);
			mCube[x * 100 + y * 10 + z].mixTranslateToRotateMatrix();
		}
		}
		}
	}

	public void initialize() {
		mHandler.post(mInit);
		mPoints.initialize();
		mChangeScene = false;
		mAngleX = 0.0f;
		mAngleY = 0.0f;
		mCubeSize = CUBE_SIZE;

		mCamera.eye[0] = CAMERA_EYE_X;
		mCamera.eye[1] = CAMERA_EYE_Y;
		mCamera.eye[2] = CAMERA_EYE_Z;
		mCamera.target[0] = 0.0f;
		mCamera.target[1] = 0.0f;
		mCamera.target[2] = 0.0f;
		mCamera.up[0] = 0.0f;
		mCamera.up[1] = 1.0f;
		mCamera.up[2] = 0.0f;
	}

	public void update(int sceneIndex) {

		if (mCameraState == CAMERA_STATE.DEMO.ordinal()) {

			if (mCubeSizeFlag) {
				mCubeSize -= 0.01f;
			} else {
				mCubeSize += 0.01;
			}
			if (mCubeSize >= 1.0f) {
				mCubeSizeFlag = true;
			}
			if (mCubeSize <= 0.1f ) {
				mCubeSizeFlag = false;
			}
			for (int i = 0; i < mCube.length; ++i) {
				mCube[i].set_size(mCubeSize,mCubeSize,mCubeSize);
			}

			mAngleX += 2.0f;
			if (mAngleX >= 360.0f) {
				mAngleX = 0.0f;
			}
			mAngleY += 1.0f;
			if (mAngleY >= 360.0f) {
				mAngleY = 0.0f;
			}

			if (sceneIndex == 0) {
				sceneCamera = 1.0f;
			} else if (sceneIndex == 1) {
				sceneCamera = 4.0f;
			}
			mCamera.eye[0] = CAMERA_EYE_X * sceneCamera * (float)Math.cos(mAngleX * 3.14f / 180.0f);
			mCamera.eye[2] = CAMERA_EYE_Z * sceneCamera * (float)Math.sin(mAngleX * 3.14f / 180.0f);
			mCamera.eye[1] = CAMERA_EYE_Y * sceneCamera * (float)Math.cos(mAngleY * 3.14f / 180.0f);
		}
		if (mShakeFlag) {
			mShakeWidth += 0.001f;
		} else {
			mShakeWidth -= 0.001f;
		}
		if (mShakeWidth >= 0.03f) {
			mShakeFlag = true;
		}
		if (mShakeWidth <= -0.03f) {
			mShakeFlag = false;
		}
	}

	@Override
	public void onClick(View v) {

		if (v.getTag().equals(mStartButtonName)) {
			if (mCameraState == CAMERA_STATE.DEMO.ordinal()) {
				mCamera.eye[0] = CAMERA_EYE_X;
				mCamera.eye[1] = CAMERA_EYE_Y;
				mCamera.eye[2] = CAMERA_EYE_Z;
				mCamera.target[0] = 0.0f;
				mCamera.target[1] = 0.0f;
				mCamera.target[2] = 0.0f;
			}
			if (mCameraState >= CAMERA_STATE.MAX.ordinal()) {
				mCameraState = 0;
			}
		}


	}

	@Override
	public boolean onTouch(View v, MotionEvent me) {
		if (v.getTag().equals(mStartButtonName)) {
			mChangeScene = true;
			mBaseLayout.removeAllViews();
			for (int i = 0; i < mCube.length; ++i) {
				mCube[i].set_size(CUBE_SIZE,CUBE_SIZE,CUBE_SIZE);
			}

		}

		return false;
	}

	@Override
	public Shape[] getShape() {
		return mCube;
	}

	@Override
	public Camera getCamera() {
		return mCamera;
	}

	@Override
	public boolean isChangeScene() {
		return 	mChangeScene;
	}

	@Override
	public Point getPoint() {
		return mPoints;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}
}